var form = document.forms["form"],
	psw1 = 0,
	psw2 = 0;
// 验证码按钮的实现
form.button.onclick = function() {
	// 首先判断手机号输入文本框是不是为空
	if(form.name.value.length === 0) {
		form.name.nextElementSibling.innerText = "手机号码不能为空";
	// 其次判断手机号输入文本框输入内容是否为合法手机号 
	} else if(form.name.value.length !== 11) {
		form.name.nextElementSibling.innerText = "请输入合法手机号";
	} else {
	// 通过前两次判断后 进来第一件事是清除span里的内容
		form.name.nextElementSibling.innerText = "";
	// 接着要按钮暂时不能被点击
		this.disabled = true;
		var seconds = 60,
			timer = setInterval(function() {
			// 判断时间是不是到最后一秒了 
				if(seconds === 1) {
					// 要是到达最后一秒要清除计时器
					clearInterval(timer);
					form.button.innerText = "获取验证码";
					// 然后将按钮设为可以点击
					form.button.disabled = false;
					return;
			}
				form.button.innerText = `重新获取(${--seconds})`
			},1000)
	}	
}
form.password1.onblur = function() {
	form.password1.nextElementSibling.innerText = form.password1.value.length === 0 ? "密码设置不能为空！" : form.password1.value.length < 6 ? "密码设置的长度是6-12" : form.password1.value.length >12 ? "密码设置的长度是6-12" : "";
	psw1 = parseInt(form.password1.value);
}
form.password2.onblur = function() {
	form.password2.nextElementSibling.innerText = form.password2.value.length === 0 ? "密码设置不能为空！" : form.password2.value.length < 6 ? "密码设置的长度是6-12" : form.password2.value.length >12 ? "密码设置的长度是6-12" : "";
	psw2 = parseInt(form.password2.value);
}
// 登录按钮的实现
var btnEnroll= document.querySelector("button.btn-enroll");
btnEnroll.onclick = function() {
	if(form.yzm.value.length === 0) {
		form.yzm.nextElementSibling.nextElementSibling.innerText = "验证码不能为空.."
	} else if(!form.yzm.value === 123456) {
	 	form.yzm.nextElementSibling.nextElementSibling.innerText = "请输入正确的验证码.."
	// } else if(!document.querySelector("input.checkbox:not(checked)") === null) {
	// 	form.yzm.nextElementSibling.nextElementSibling.innerText = "请勾选私密政策和用户协议"
	} else if( psw1 !== psw2){
		form.password1.nextElementSibling.innerText = "两次输入的密码不一样";
		form.password2.nextElementSibling.innerText = "两次输入的密码不一样";
	} else if (form.tp.value != code) {
		form.tp.nextElementSibling.nextElementSibling.innerText = "图片验证码输入错误"
	}
	 else {
		form.yzm.nextElementSibling.nextElementSibling.innerText = "";
		form.password1.nextElementSibling.innerText = "";
		form.password2.nextElementSibling.innerText = "";
		window.location.href = "https://www.baidu.com/";
	}
}
var letters = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","0","1","2","3","4","5","6","7","8","9"];
var code = "";
function genterateCode() {
	code = "";
	for(var i = 1; i <= 4; i++) {
		code += letters[Math.floor(Math.random()* letters.length)];
		document.querySelector("span.code").innerText = code;
	}
}
genterateCode();
document.querySelector("span.code").onclick = function() {
	genterateCode();
}